package com.mogul.lahub.fragment

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.graphics.TypefaceCompat
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.hjq.shape.view.ShapeTextView
import com.mogul.lahub.DensityUtil
import com.mogul.lahub.MainActivity
import com.mogul.lahub.MyItemDecoration
import com.mogul.lahub.R
import com.mogul.lahub.databinding.FragmentInilhbtLoanBinding

class InitLoanFragment:Fragment() {

    private lateinit var initLoanBinding:FragmentInilhbtLoanBinding

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        initLoanBinding = FragmentInilhbtLoanBinding.inflate(inflater,container,false)
        return initLoanBinding.root
    }

    @SuppressLint("RestrictedApi")
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val mainActivity = context as MainActivity
        val typeface =
            TypefaceCompat.createFromResourcesFontFile(mainActivity, getResources(), R.font.xuyuan, "", 0)
        initLoanBinding.tvNameValueMain.setTypeface(typeface)

        initLoanBinding.rvLoanTenurListMain.layoutManager = GridLayoutManager(mainActivity, 3)
        initLoanBinding.rvLoanTenurListMain.addItemDecoration(
            MyItemDecoration(
                DensityUtil.dip2px(
                    mainActivity,
                    23f
                ),0
            )
        )
        initLoanBinding.rvLoanTenurListMain.adapter = LoanTenurAdapter(mainActivity)
    }

    class LoanTenurAdapter(mContext: Context) :
        RecyclerView.Adapter<LoanTenurAdapter.MyViewHolder>() {

        private var selectPosition = 0
        private var arrayList = arrayListOf("30天","90天","180天")

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
            val view =
                LayoutInflater.from(parent.context)
                    .inflate(R.layout.item_loan_tenur_home, parent, false)
            return MyViewHolder(view)
        }

        override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
            holder.mTextView.setText(arrayList[position])
            if (selectPosition==position) {
                holder.mTextView.shapeDrawableBuilder
                    .setSolidColor(-0x3af6).intoBackground()
                holder.mTextView.setTextColor(Color.WHITE)
            }else {
                holder.mTextView.shapeDrawableBuilder
                    .setSolidColor(Color.WHITE).intoBackground()
                holder.mTextView.setTextColor(Color.BLACK)
            }
            holder.mTextView.setOnClickListener {
                if (selectPosition != holder.adapterPosition) {
                    selectPosition = holder.adapterPosition
                    notifyDataSetChanged()
                }
            }
        }

        override fun getItemCount(): Int {
            return 3
        }

        class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
            var mTextView: ShapeTextView

            init {
                mTextView = itemView as ShapeTextView
            }
        }
    }
}